#include "QtBox2DView.h"
#include <QGraphicsScene>
#include <QMenu>
#include <QContextMenuEvent>
#include <QList>
#include <QtCore/QSettings>
#include <QFile>
#include <QtCore/QDir>
#include <QtGui/QMessageBox>
#include "qtbox2dtest.h"
#include "QtImageDialog.h"
#include "QtBox2DScene.h"

QtBox2DView::QtBox2DView( QWidget * parent /*= 0*/ ):
    QGraphicsView(parent)
{
	setAcceptDrops(true);
	
		setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


		mImageDialog = new QtImageDialog(this);
		mImageDialog->initIcons();
}

QtBox2DView::QtBox2DView( QGraphicsScene * scene, QWidget * parent /*= 0*/ ):
	QGraphicsView(scene, parent)
{
	setAcceptDrops(true);
	
		setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


		mImageDialog = new QtImageDialog(this);
		mImageDialog->initIcons();
		connect(this, SIGNAL(mousePos(const QPoint&)), scene, SLOT(setMousePos(const QPoint&)));
}

QtBox2DView::~QtBox2DView(void)
{

}

void QtBox2DView::contextMenuEvent ( QContextMenuEvent * event )
{
	if (((QtBox2DScene*)scene())->isStart()) 
	{
		return;
	}

	emit mousePos(event->pos());
	QtBox2DTest* pWidget = static_cast<QtBox2DTest*>(parentWidget());
	if(itemAt(event->pos()) != 0 && pWidget)
	{
		pWidget->getEditMenu()->exec(event->globalPos());
	}
	else if (pWidget)
	{
		pWidget->getCreateMenu()->exec(event->globalPos());
	}
}

void QtBox2DView::resizeEvent ( QResizeEvent * event )
{
	QGraphicsView::resizeEvent(event);
	scene()->setSceneRect(rect());
}


void QtBox2DView::showImageDlg( )
{
	mImageDialog->show();
}
